//
//  ProtractorView.m
//  量角器
//
//  Created by zhaoyf on 2021/8/3.
//

#import "ProtractorView.h"
#define kMiniDistance 30
#define kMaxPointsNum 8
#define gridValue 1
#define UIColorFromRGB(rgbValue) ([UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0])

#define normalColor UIColorFromRGB(0x64b0e1)
#define illegalColor UIColorFromRGB(0xFF0000)
#define pi 3.14159265358979323846
@implementation ProtractorView
- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    [self drawCoverageView];
}
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        MNPoint *oo = [[MNPoint alloc] initWithX:110 andY:110];
        MNPoint *tt = [[MNPoint alloc] initWithX:210 andY:110];
        MNPoint *rr = [[MNPoint alloc] initWithX:110 andY:260];
        self.pointsArray = [[NSMutableArray alloc] initWithArray:@[oo,tt,rr]];
        [self addSubview:self.oneView];
        [self addSubview:self.twoView];
        [self addSubview:self.threeView];
    }
    return self;
}


-(TheMoveView *)oneView
{
    if (_oneView == nil) {
        
        _oneView = [[TheMoveView alloc] initWithFrame:CGRectMake(100, 100, 20, 20)];
//        _oneView.startPoint = CGPointMake(110, 110);
        _oneView.backgroundColor = [UIColor redColor];
        __weak __typeof__(self) weakSelf = self;
        _oneView.moveBlock = ^(MNPoint * _Nonnull Mpoint) {
            [weakSelf.pointsArray replaceObjectAtIndex:0 withObject:Mpoint];
            CGPoint pp = CGPointMake(Mpoint.x, Mpoint.y);
            CGPoint ii = [weakSelf gridPointAdjust:pp];
            [weakSelf.movePoint setPoint:CGPointMake(ii.x, ii.y)];
            [weakSelf.pointsArray replaceObjectAtIndex:0 withObject:[[MNPoint alloc] initWithX:ii.x andY:ii.y]];
            [weakSelf setNeedsDisplay];
            [weakSelf setNeedsLayout];
            CGPoint one = [weakSelf.pointsArray[0] getPoint];
            CGPoint two = [weakSelf.pointsArray[1] getPoint];
            CGPoint three = [weakSelf.pointsArray[2] getPoint];
            [weakSelf getAnglesWithThreePoint:one pointB:two pointC:three];
        };
        _oneView.layer.masksToBounds = YES;
        _oneView.layer.cornerRadius = 10;
    }
    return _oneView;
}

-(TheMoveView *)twoView
{
    if (_twoView == nil) {
        _twoView = [[TheMoveView alloc] initWithFrame:CGRectMake(200, 100, 20, 20)];
//        _twoView.startPoint = CGPointMake(210, 110);
        _twoView.backgroundColor = [UIColor yellowColor];
        __weak __typeof__(self) weakSelf = self;
        _twoView.moveBlock = ^(MNPoint * _Nonnull Mpoint) {
            CGPoint pp = CGPointMake(Mpoint.x, Mpoint.y);
            CGPoint ii = [weakSelf gridPointAdjust:pp];
            [weakSelf.movePoint setPoint:CGPointMake(ii.x, ii.y)];
            [weakSelf.pointsArray replaceObjectAtIndex:1 withObject:[[MNPoint alloc] initWithX:ii.x andY:ii.y]];
            [weakSelf setNeedsDisplay];
            [weakSelf setNeedsLayout];
            CGPoint one = [weakSelf.pointsArray[0] getPoint];
            CGPoint two = [weakSelf.pointsArray[1] getPoint];
            CGPoint three = [weakSelf.pointsArray[2] getPoint];
            [weakSelf getAnglesWithThreePoint:one pointB:two pointC:three];
        };
        _twoView.layer.masksToBounds = YES;
        _twoView.layer.cornerRadius = 10;
    }
    return _twoView;
}

-(TheMoveView *)threeView
{
    if (_threeView == nil) {
        _threeView = [[TheMoveView alloc] initWithFrame:CGRectMake(100, 250, 20, 20)];
//        _threeView.startPoint = CGPointMake(110, 260);
        _threeView.backgroundColor = [UIColor greenColor];
        __weak __typeof__(self) weakSelf = self;
        _threeView.moveBlock = ^(MNPoint * _Nonnull Mpoint) {
            [weakSelf.pointsArray replaceObjectAtIndex:2 withObject:Mpoint];
            CGPoint pp = CGPointMake(Mpoint.x, Mpoint.y);
            CGPoint ii = [weakSelf gridPointAdjust:pp];
            [weakSelf.movePoint setPoint:CGPointMake(ii.x, ii.y)];
            [weakSelf.pointsArray replaceObjectAtIndex:2 withObject:[[MNPoint alloc] initWithX:ii.x andY:ii.y]];
            [weakSelf setNeedsDisplay];
            [weakSelf setNeedsLayout];
            CGPoint one = [weakSelf.pointsArray[0] getPoint];
            CGPoint two = [weakSelf.pointsArray[1] getPoint];
            CGPoint three = [weakSelf.pointsArray[2] getPoint];
            [weakSelf getAnglesWithThreePoint:one pointB:two pointC:three];
        };
        _threeView.layer.masksToBounds = YES;
        _threeView.layer.cornerRadius = 10;
    }
    return _threeView;
}
- (void)drawCoverageView {
    
    
    ctx = UIGraphicsGetCurrentContext();
    CGContextBeginPath(ctx);
    CGContextSetLineWidth(ctx, 2);
    CGContextSetLineJoin(ctx, kCGLineJoinRound);
    

    CGContextSetStrokeColorWithColor(ctx, [[UIColor blueColor] CGColor]);
    
    [_pointsArray enumerateObjectsUsingBlock:^(MNPoint * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        CGPoint point = [[self.pointsArray objectAtIndex:idx] getPoint];
        if (idx == 0) {
            CGContextMoveToPoint(ctx, point.x, point.y);
        }
        else if(idx == 1 || idx == 2){
            CGContextAddLineToPoint(ctx, point.x, point.y);
        }
    }];
//    CGContextClosePath(ctx);

    CGContextStrokePath(ctx);
    CGContextSetFillColorWithColor(ctx, [UIColor blueColor].CGColor);
    CGContextSetStrokeColorWithColor(ctx, [UIColor blueColor].CGColor);
    
    CGContextBeginPath(ctx);
    
    CGContextSetFillColorWithColor(ctx, [UIColor blueColor].CGColor);
    CGContextDrawPath(ctx, kCGPathFillStroke);
    
    CGContextStrokePath(ctx); //直接在图形上下文中渲染路径
    CGContextDrawPath(ctx, kCGPathFillStroke);
}

- (CGFloat)getAnglesWithThreePoint:(CGPoint)pointA pointB:(CGPoint)pointB pointC:(CGPoint)pointC {
    
    CGFloat x1 = pointA.x - pointB.x;
    CGFloat y1 = pointA.y - pointB.y;
    CGFloat x2 = pointC.x - pointB.x;
    CGFloat y2 = pointC.y - pointB.y;
    CGFloat x = x1 * x2 + y1 * y2;
    CGFloat y = x1 * y2 - x2 * y1;
    CGFloat angle = acos(x/sqrt(x*x+y*y));
    NSLog(@"%f",180 * (angle/pi));
    if (self.angleBlock) {
        self.angleBlock(180 * (angle/pi));
    }
    return angle;
}

- (CGPoint)gridPointAdjust:(CGPoint)point {
    int gridCheckX = (int)point.x % gridValue;
    int gridCheckY = (int)point.y % gridValue;
    if (gridCheckX != 0) {
        point.x = ((int)point.x / gridValue) * gridValue;
    }
    if (gridCheckY != 0) {
        point.y = ((int)point.y / gridValue) * gridValue;
    }
    return point;
}
@end
